package fr.charlott.core.repository.model;

import java.util.List;

import javax.persistence.Cacheable;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.apache.commons.lang.StringUtils;

@Entity
@Table(name="Produit", uniqueConstraints = {@UniqueConstraint(columnNames={"reference"})})
@Embeddable
@Cacheable
public class Produit {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	
	private String reference;
	
	private String nom;
	
	private double prix;
	
	private Collection collection;
	
	@OneToOne
	private Categorie categorie;
	
	@OneToOne
	private Saison saison;
	
	@ManyToMany(mappedBy="produits")
	private List<Personne> personnes;

	public Produit(){
		super();
	}
	
	public Produit(Saison saison) {
		super();
		this.saison = saison;
	}
	
	@Override
	public int hashCode() {
		return referenceToInt(reference);
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Produit other = (Produit) obj;
		return (referenceToInt(reference) == referenceToInt(other.reference));
	}
	
	private int referenceToInt(String reference){
		return Integer.valueOf(StringUtils.remove(reference, "."));
	}
	
	// setter - getter
	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	public Collection getCollection() {
		return collection;
	}

	public void setCollection(Collection collection) {
		this.collection = collection;
	}

	public double getPrix() {
		return prix;
	}

	public void setPrix(double prix) {
		this.prix = prix;
	}

	public List<Personne> getPersonnes() {
		return personnes;
	}

	public void setPersonnes(List<Personne> personnes) {
		this.personnes = personnes;
	}

	public Saison getSaison() {
		return saison;
	}

	public void setSaison(Saison saison) {
		this.saison = saison;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getReference() {
		return reference;
	}

	public void setReference(String reference) {
		this.reference = reference;
	}

	public Categorie getCategorie() {
		return categorie;
	}

	public void setCategorie(Categorie categorie) {
		this.categorie = categorie;
	}

}
